#include "CustomerCollection.h"
#include <iostream>
#include <string>

using namespace std;

CustomerCollection::CustomerCollection(void) : size(0), head(NULL) {

}

CustomerCollection::~CustomerCollection(void) {

}

int CustomerCollection::count() {
	return size; // returns the size of the customer list
}

int CustomerCollection::addCustomer(Customer *item) {
	// adds a new customer to the next node in the linked list
	Customer *itemPointer = new Customer;
	itemPointer = item;
	itemPointer->setNext(head);
	head = itemPointer;
	return size++;
}

Customer *CustomerCollection::retrieveCustomer(int position) {
	Customer *currentPosition = head;
	// work backwards
	for (int i = count() - 1; i > position && currentPosition != NULL; i--) {
		currentPosition = currentPosition->getNext();
	}
	return currentPosition; // gets position of customer
}

bool CustomerCollection::deleteCustomer(int position) {
	// deletes a customer from a position within the list
	if (retrieveCustomer(position) == NULL) {
		return false;
	} else {
		// node exsists
		// if previous node doesn't exsist then we are at the start
		if (retrieveCustomer(position-1) == NULL) {
			// fix the beginning pointer to the current next pointer
			head->setNext(retrieveCustomer(position+1));
		} else {
			// fix the previous pointer to the next next pointer
			retrieveCustomer(position-1)->setNext(retrieveCustomer(position+1));
		}

		size--;
		return true;
	}
}
